package logger

import (
	"github.com/sirupsen/logrus"
	"os"
)

type Configuration struct {
	LogFile string `yaml:"LogFile"`
}

type Logger struct {
	conf *Configuration
}

func NewLogger(conf Configuration) *Logger {
	logger := &Logger{
		conf: &conf,
	}
	if len(conf.LogFile) > 0 {
		logger.setLogFile(conf.LogFile)
	}
	return logger
}

func (l *Logger) setLogFile(file string) {
	if file, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); err == nil {
		logrus.SetOutput(file)
	} else {
		logrus.Fatal(err)
	}
}

func (l *Logger) AddHook(hook logrus.Hook) {
	logrus.AddHook(hook)
}
